<?



class CTRLinit extends CONTROL {

#	In parent class:
#	function asset()

	function __construct( $vars ) {
		
		$this->_addvars( $vars );
		$this->tools = MOD_CONSTRUCT::request_json( engine_get( 'dashboard.tools.id' ));
	}
	
#	Engine Pages.
	function filter_pages() {

		if( $eid  = $this->tools->extend->init )
			$init = "if( !init.fresh ) {
		
			setTimeout( dashboard.showhide.bind( $( '$eid' )), 12 );		
			init.fresh = 1;
		}";
		

		return <<<EOT
		
		ref.seed([
			nav = new Element( "span", { "class": "navigation" }),
			new Element( "h1" ).update( "Engine " ).insert( new Element( "strong" ).update( "pages" )),
			box = new Dropbox({ descr:'Sitemap', type:'page', action:'win', blank:'add page', id:0, floats:{$this->render_control( 0 )} }),
			new Elview({ type: 'module', descr: 'Bekijk alle modules', opts:{ 'overview':{ type:'module', action:'list', reqs:{}, flow:{ limit:24 }}}})
		], true );
		
		nav.insert( new Element( "span" ).update( "elements" ).observe( "click", ref.request.bind( ref, { type:'init', action:'elements' })));
		nav.insert( new Element( "span" ).update( "groups" ).observe( "click", ref.request.bind( ref, { type:'init', action:'groups' })));
		
		box.sortable( "page" );
		
		
		/*
		
function DragHandler( e ) {
	console.log( e.element() )
	
	//e.dataTransfer.setData('Text', this.id);
}
 
function DropHandler( e ) {
	console.log( "foo." )
	console.log( e )
	
	//var id = e.dataTransfer.getData('Text');
	//this.appendChild(document.getElementById(id));
	//e.preventDefault();
	
	
	
}

ref.insert( new Element( "li", { draggable: "true" }).update("drag").observe("dragstart", DragHandler ));
ref.insert( new Element( "ul", { ondragenter:"return false", ondragover:"return false", style: "background:#eee; width:200px; height:80px; border:1px dotted #ddd;" }).observe("drop", DropHandler ));
	*/	
		
		$init

EOT;

	}
	
	function filter_elements() {
		global $DB;
		
		if( $tools = json_decode( $DB->query_direct( "select json from moddump where ID=" . engine_get('dashboard.tools.id' ))))
			foreach( $tools->elements as $el ) $lines[] = ( $el )?
				"new Elview({ action: 'new', func:'window', type: '{$el->type}', descr: '{$el->descr}', opts:{ 'overview':{ type:'{$el->type}', action:'list', reqs: " . ( $el->reqs? json_encode( $el->reqs ) : "{active:1}" ) . ", flow:{ limit:24 }}, 'dump':{ type:'{$el->type}', action:'list', reqs:{ active:0 }, flow:{ limit:24 }}, 'search':{ type:'{$el->type}', action:'search', reqs:{ active:1 }, flow:{ limit:24 }}}})" : "br";

		return "
		
		ref.seed([
			nav = new Element( 'span', { 'class': 'navigation' }),
			new Element( 'h1' ).update( 'Engine ' ).insert( new Element( 'strong' ).update( 'elements' )),
			" . implode( ',', $lines ) . "
		], true );
		
		nav.insert( new Element( 'span' ).update( 'pages' ).observe( 'click', ref.request.bind( ref, { type:'init', action:'pages' })));
		nav.insert( new Element( 'span' ).update( 'groups' ).observe( 'click', ref.request.bind( ref, { type:'init', action:'groups' })));
		";

	}

	function filter_groups() {
		global $DB;
		
		if( $tools = json_decode( $DB->query_direct( "select json from moddump where ID=" . engine_get('dashboard.tools.id' ))))
			foreach( $tools->groups as $el ) $lines[] = ( $el )?
				"new Elview({ action: 'new', func:'window', type: '{$el->type}', descr: '{$el->descr}', opts:{ 'overview':{ type:'{$el->type}', action:'list', reqs:{ active:1 }, flow:{ limit:12 }}, 'dump':{ type:'{$el->type}', action:'list', reqs:{ active:0 }, flow:{ limit:12 }}}})" : "br";

		/* can be user to manage single files ,
			new Elview({ type: 'media.file', descr: 'Alle bestanden:', opts:{ 'overview':{ type:'media.file', action:'list', reqs:{ active:1 }, flow:{ limit:24 }}, 'dump':{ type:'media.file', action:'list', reqs:{ active:0 }, flow:{ limit:24 }}}}) */

		return "
		
		ref.seed([
			nav = new Element( 'span', { 'class': 'navigation' }),
			new Element( 'h1' ).update( 'Engine ' ).insert( new Element( 'strong' ).update( 'groups' )),
			" . implode( ',', $lines ) . ",
			new Elview({ type: 'media.img', descr: 'Alle beelden:', opts:{ 'overview':{ type:'media.img', action:'list', reqs:{ active:1 }, flow:{ limit:24 }}, 'dump':{ type:'media.img', action:'list', reqs:{ active:0 }, flow:{ limit:24 }}}}),
			new Elview({ type: 'media.file', descr: 'Alle bestanden:', opts:{ 'overview':{ type:'media.file', action:'list', reqs:{ active:1 }, flow:{ limit:24 }}, 'dump':{ type:'media.file', action:'list', reqs:{ active:0 }, flow:{ limit:24 }}}}),
			new Elview({ type: 'media.stream', descr: 'Alle streams:', opts:{ 'overview':{ type:'media.stream', action:'list', reqs:{ active:1 }, flow:{ limit:24 }}, 'dump':{ type:'media.stream', action:'list', reqs:{ active:0 }, flow:{ limit:24 }}}}),
			new Elview({ type: 'user', descr: 'Alle users:', opts:{ 'overview':{ type:'user', action:'list', reqs:{ active:1 }, flow:{ limit:24 }}, 'dump':{ type:'user', action:'list', reqs:{ active:0 }, flow:{ limit:24 }}}}),
			new Elview({ type: 'social', descr: 'Alle sociale toevoegingen:', opts:{ 'overview':{ type:'social', action:'list', reqs:{ active:1 }, flow:{ limit:24 }}, 'dump':{ type:'social', action:'list', reqs:{ active:0 }, flow:{ limit:24 }}}}), br,
			new More({ descr:'Bekijk agenda', extend:'cal' })
		], true );
		
		nav.insert( new Element( 'span' ).update( 'pages' ).observe( 'click', ref.request.bind( ref, { type:'init', action:'pages' })));
		nav.insert( new Element( 'span' ).update( 'elements' ).observe( 'click', ref.request.bind( ref, { type:'init', action:'elements' })));
		";
		
	}
	
	
#	Admin request.
	function filter_admin() {
		global $DB;
		
		if( $settingslist = $DB->query( "select ID, `key`, `value` from settings where active" )) //$DB->filter_key( "settings", "active = 1", array( "ID", "`key`", "`value`" )))
			while( $row = mysql_fetch_array( $settingslist, MYSQL_ASSOC ))
				$settings[ $row[ 'key' ]] = $row;
				
		if( $typeslist = $DB->query( "select `key`, `meta` from types where active" )) //$DB->filter_key( "settings", "active = 1", array( "ID", "`key`", "`value`" )))
			while( $row = mysql_fetch_array( $typeslist, MYSQL_ASSOC ))
				$typeslgd[ $row[ 'key' ]] = (array) json_decode( $row[ 'meta' ]);
				
		
		return json_encode( array( "settings"=> $settings, "typeslgd"=> $typeslgd ));
		
		//unset( $_SESSION['init']['dictionary']->{'id'} );
		//return json_encode( $_SESSION['init']['dictionary'] );
	}
	
	
	function render_control( $id ) {
	
		if( $id ) {
			$Page = new PAGE( $id );
			$node = $Page->render_core_json( false );
			
			if( $subs = $Page->get( 'sub' ))
				foreach( explode( ",", $subs ) as $sub )
					if( $vars = $this->render_control( (int)$sub ) ) $node['floats'][] = $vars;

			return $node;
	
		} else {
			foreach( $_SESSION['map']['menu'] as $node )
				if( $vars = $this->render_control(( int ) $node['ID'] )) $map[] = $vars;
			return json_encode( $map );
		}
	}


}






/*

#	Object INIT.

if( $Request->action == "pages" ) {

		?>
		
		ref.seed([
			new Element( "span", { "class": "navigation" }).insert( new Element( "span" ).update( "elements" ).observe( "click" )).insert( new Element( "span" ).update( "groups" )),
			new Element( "h1" ).update( "Engine " ).insert( new Element( "strong" ).update( "pages" )),
			box = new Dropbox({ descr:'Sitemap', type:'page', action:'win', blank:'add page', id:0, floats:<?=filter_control( 0 )?> })
		], true );
		
		box.sortable( "page" );
		
	<?
	
} else if( $Request->action == "win.elements" ) {

	?>
		
		ref.seed([
			new Element( "span", { "class": "navigation" }).insert( new Element( "span" ).update( "pages" )).insert( new Element( "span" ).update( "groups" )),
			new Element( "h1" ).update( "Engine " ).insert( new Element( "strong" ).update( "elements" )),
			new Button( { id:ref.id+".btn.newsnew", descr: "add newspost", func:"window", tool:"text.news", action:"edit.new", meta: {descr: "breaking news"}} ),
		], true );
		
		$(ref.id+'.btn.newsnew').observe('click', function(e){ $('column_right').insert( new Win("asset.group.news.new").request({tool:"text.news", action:"edit.new", vars: {descr: "breaking news"}}) ); });
		
	<?
}*/